#include "GLWindow.h"

#include <assert.h>

#include <GL/glut.h>    // Header File For The GLUT Library
#include <GL/gl.h>	// Header File For The OpenGL32 Library
#include <GL/glu.h>	// Header File For The GLu32 Library

#define WINDOW_WIDTH 512
#define WINDOW_HEIGHT 512

GLWindow::GLWindow( const std::string& title )
{
    m_width = WINDOW_WIDTH;
    m_height = WINDOW_HEIGHT;
    m_title = title;
}

GLWindow::~GLWindow( void ) {

}

int GLWindow::Width( void ) {
    return m_width;
}

int GLWindow::Height( void ) {
    return m_height;
}

void GLWindow::Init( int argc, char *argv[] ) {
    glutInit( &argc, argv );
    // set windows size
    glutInitWindowSize( WINDOW_WIDTH, WINDOW_HEIGHT );
    // set OpenGL display mode
    glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
    // set window title
    glutCreateWindow( m_title.c_str() );
    // set rendering function
    glutDisplayFunc( Render );
}

void GLWindow::Start( void ) {
    glutMainLoop();
}

void GLWindow::Render( void ) {
    // Clear the Color and Depth Buffers
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    // Orthogonal Projection Matrix
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();

    // set the orthographic mode parameters of opengl to match the
    // screen dimensions to prevent cheating in screen space calculations
    glOrtho( 0, WINDOW_WIDTH, 0, WINDOW_HEIGHT, 0, 1 );

    // TODO: insert draw code here.

    // Flush the OpenGL Rendering Pipeline
    glFlush();

    // Swap the OpenGL Drawing Frame Buffer with the OpenGL Visible Frame Buffer
    glutSwapBuffers();
}

